package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerDao customerDao;

    @Override
    public String getList(Integer page, Integer rows, String customerName) {
        List<Customer> list = customerDao.getList(page, rows, customerName);
        int size = list.size();
        Map<String,Object> map = new HashMap<>();
        map.put("total",size);
        map.put("rows",list);
        Gson gson = new Gson();
        String result = gson.toJson(map);
        return result;
    }

    @Override
    public String save(Customer customer, String customerId) {
        Integer update = null;
        Integer insert = null;
        ServiceVO serviceVO = null;
        if (customerId!=null){
            update = customerDao.update(customer.getCustomerName(),customer.getContacts(),customer.getPhoneNumber(),customer.getAddress(),customer.getRemarks(),customerId);
        }else {
            insert = customerDao.insert(customer);
        }
        if (update!=null){
            serviceVO = new ServiceVO(100,"修改成功");
        }
        if (insert!=null){
            serviceVO = new ServiceVO(100,"添加成功");
        }
        Gson gson = new Gson();
        String result = gson.toJson(serviceVO);
        return result;
    }

    @Override
    public String delete(String[] ids) {
        Integer delete = null;
        ServiceVO serviceVO = null;
        if (ids.length!=0){
            delete = customerDao.delete(ids);
        }
        if (delete!=null){
            serviceVO = new ServiceVO(100,"删除成功");
        }
        Gson gson = new Gson();
        String result = gson.toJson(serviceVO);
        return result;
    }
}
